﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;

using NORM.Entity;
using NORM.SQLObject;
using NORM.Common;
using NORM.Configure;

namespace NORM.DataBase
{
    public class DataBase : IDisposable
    {
        protected string _sql = string.Empty;
        protected string _connectionString = string.Empty;
        protected OsqlCommand Command = new OsqlCommand();
        protected DataBaseTypes _dataBaseType = DataBaseTypes.SqliteDataBase;

        /// <summary>
        /// 清理sqlstatement
        /// </summary>
        internal void sqlstatementclear()
        {            
            _sql = string.Empty;
            Command = new OsqlCommand();
        }

        protected string Join(SectionString[] SectionsString)
        {
            string rvl = string.Empty;
            foreach (var SectString in SectionsString)
            {
                rvl += SectString.ToString() + ";";
            }
            return rvl;
        }

        protected SectionString[] Parse(string ParseString)
        {
            List<SectionString> array = new List<SectionString>();
            string[] splits = ParseString.Split(';');
            foreach (string str in splits)
            {
                if (!string.IsNullOrEmpty(str))
                {
                    SectionString secstr = new SectionString(str);
                    if (secstr.Key.Equals("Data Source"))
                    {
                        secstr.Value = secstr.Value.Replace("|DataDirectory|", AppDomain.CurrentDomain.BaseDirectory);
                    }
                    if (secstr.Key.Equals("Password") || secstr.Key.Equals("Pwd"))
                    {
                        secstr.Value = EncryptDES.DecryptDES(secstr.Value);
                    }
                    array.Add(secstr);
                }

            }
            return array.ToArray();
        }

        protected void validateOqlend(OQL oql)
        {
            if (!oql.IsEnd)
            {
                oql = oql.End;
                throw new NORMException(ExceptionType.OQLExceptin, "请检查是否调用End");
            }              
        }

        public override string ToString()
        {
            return _sql;
        }

        /// <summary>
        /// 
        /// </summary>
        public string ConnectionString
        {
            get { return _connectionString; }
        }
        
        /// <summary>
        /// 
        /// </summary>
        public DataBaseTypes DataBaseType
        {
            internal set { _dataBaseType = value; }
            get { return _dataBaseType; }
        }

        /// <summary>
        /// 开启事务
        /// </summary>
        public virtual void BeginTransaction() { }

        /// <summary>
        /// 提交事务
        /// </summary>
        /// <returns></returns>
        public virtual int Commit() { return 1; }

        /// <summary>
        /// 事务回滚
        /// </summary>
        public virtual void RollBack() { }

        /// <summary>
        /// 释放资源
        /// </summary>
        public virtual void Dispose()
        {
            CacheProperties.cacheInstance.Dispose();
        }

        public virtual int Execute(OQL oql) { return -1; }

        public virtual bool Insert<T>(T model) where T : EntityBase { return false; }

        public virtual bool Update<T>(T model) where T : EntityBase { return false; }

        public virtual bool Delete<T>(T model) where T : EntityBase { return false; }

        public virtual bool Delete<T>(object PrimaryKeyValue) where T : EntityBase { return false; }

        public virtual List<T> QueryList<T>(OQL oql, PageLimit page) where T : EntityBase { return new List<T>(); }

        public virtual List<T> QueryList<T>(OQL oql) where T : EntityBase { return new List<T>(); }

        public virtual T QueryObject<T>(OQL oql) where T : EntityBase { return (T)Activator.CreateInstance(typeof(T)); }

        public virtual T QueryModel<T>(object PrimaryKeyValue) where T : EntityBase { return (T)Activator.CreateInstance(typeof(T)); }

        public virtual DataTable QueryTable(OQL oql, PageLimit page = null) { return new DataTable(); }

        public virtual int Execute(CommandType cmdType, string cmdText, params DbParameter[] cmdParms) { return -1; }

        public virtual DataSet GetDataSet(CommandType cmdType, string cmdText, params DbParameter[] cmdParms) { return new DataSet(); }
                
        public virtual DataTable GetDataBaseObject(string name, DataBaseSchema schema) { return new DataTable(); }

    }
}
